﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Database;
using Company.Database.Entities;
using Company.Database.Sqlite;
using Company.Shell.Interfaces;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class HistoryAlarmViewModel : ObservableObject
    {
        /*
         *  字段成员
         *  属性成员
         *  ICommand成员
         *  在构造器中获取的实例的成员
         *  构造器
         *  方法成员
         */
        private ObservableCollection<AlarmEntity> alarmEntities = new();
        /// <summary>
        /// 从数据库中查询历史报警数据
        /// </summary>
        public ObservableCollection<AlarmEntity> AlarmEntities
        {
            get => alarmEntities;
            set =>SetProperty(ref alarmEntities, value);
        }
        public ICommand LoadedCommand { get; }
        public ICommand SelectRoomCommand { get; }
        public IRoomManager RoomManager { get; }
        private IAlarmRepository AlarmRepository { get; }

        public HistoryAlarmViewModel(
            IRoomManager roomManager ,
            IAlarmRepository alarmRepository)
        {
            RoomManager = roomManager;
            AlarmRepository = alarmRepository;
            LoadedCommand = new AsyncRelayCommand(OnLoadedCommand);
            SelectRoomCommand = new AsyncRelayCommand(OnLoadedCommand);
        }

        private async Task OnLoadedCommand()
        {
            var result = await Task.Run(() =>
            {
                var list = AlarmRepository.GetAll().Where(t => t.RoomNumber == RoomManager.Room.Id).ToList();
                return list;
            });
            AlarmEntities.Clear();
            result?.ForEach(t=>AlarmEntities.Add(t));   
        }
    }
}
